﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using Citi.HeadCount.WebUI.Models;
using Citi.HeadCount.Domain;

namespace Citi.HeadCount.WebUI.Controllers
{
    public class ImportController : Controller
    {
        public ActionResult Upload()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Upload(HttpPostedFileBase file, string fileType)
        {
            if (!Path.GetFileName(file.FileName).StartsWith(fileType))
                return Json(new { customMessage = "File not match" }, "text/html");

            var filePath = Path.Combine(Server.MapPath("~/Files"), string.Concat(fileType, "_", Path.GetFileName(file.FileName)));
            file.SaveAs(filePath);

            var errors = new List<ErrorModel>();

            if (fileType == "DSMT")
                new Util.ImportHelper().ImportGOCData(filePath, ref errors);
            else if (fileType == "Functions")
                new Util.ImportHelper().ImportFunctionData(filePath, ref errors);
            else if (fileType == "ManagedSegment")
                new Util.ImportHelper().ImportManagedSegmentData(filePath, ref errors);
            else if (fileType == "GOCCodes")
                new Util.ImportHelper().ImportGOCCode(filePath, ref errors);
            return Json(new {customMessage = "Successfully" }, "text/html");
        }
    }
}
